package org.szy.admin.pojo;

import org.szy.core.common.entity.BaseTreeNode;
import org.szy.core.common.uilts.TreeBuildFactory;
import org.szy.plugin.sys.entity.SysMenu;

import java.util.ArrayList;
import java.util.List;

/**
 * 树形结构
 *
 * @author 孙志阳
 * @date 2023-02-14
 * @since 1.0.0
 **/
public class LoginMenuTreeNode implements BaseTreeNode<LoginMenuTreeNode> {

    /**
     * 主键
     */
    private Object id;

    /**
     * 父节点
     */
    private Object pid;

    /**
     * 名称
     */
    private String title;

    /** 图标 */
    private String icon;

    /**
     * 值
     */
    private String value;

    /**
     * 排序，越小优先级越高
     */
    private Integer weight;

    /**
     * 子节点
     */
    private List<LoginMenuTreeNode> children;

    @Override
    public Object getId() {
        return id;
    }

    public void setId(Object id) {
        this.id = id;
    }

    @Override
    public Object getPid() {
        return pid;
    }

    public void setPid(Object pid) {
        this.pid = pid;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    public List<LoginMenuTreeNode> getChildren() {
        return children;
    }

    @Override
    public void setChildren(List<LoginMenuTreeNode> children) {
        this.children = children;
    }

    public static List<LoginMenuTreeNode> buildLoginTreeNode(List<SysMenu> sysMenuList) {
        List<LoginMenuTreeNode> menuTreeNodeList = new ArrayList<>();
        sysMenuList.forEach(i -> {
            LoginMenuTreeNode loginMenuTreeNode = new LoginMenuTreeNode();
            loginMenuTreeNode.setId(i.getId());
            loginMenuTreeNode.setTitle(i.getName());
            if (0 == i.getType()) {
                loginMenuTreeNode.setValue("javascript:;");
            } else {
                loginMenuTreeNode.setValue(i.getRouter());
            }
            loginMenuTreeNode.setIcon(i.getIcon());
            loginMenuTreeNode.setWeight(i.getSort());
            loginMenuTreeNode.setPid(i.getPid());
            menuTreeNodeList.add(loginMenuTreeNode);
        });
        return new TreeBuildFactory<LoginMenuTreeNode>().doTreeBuild(menuTreeNodeList);
    }
}
